﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using Netduino4Fun.Framework.ElectronicBrick;

namespace Netduino4Fun.MorseMessengingSystem
{
    public class Program
    {
        const double LongPulse = 300;
        const double ExtraLongPulse = 1000;

        static string morseLetter;
        static string message;

        static Lcd lcd;

        public static void Main()
        {
            lcd = new Lcd();
            lcd.BlinkCursor = true;

            PushButton stopButton = new PushButton(Pins.ONBOARD_SW1);

            PulseButton morseButton = new PulseButton(Pins.GPIO_PIN_D8);
            morseButton.PulseStart += new PulseButton.PulseEventHandler(morseButton_PulseStart);
            morseButton.PulseEnd += new PulseButton.PulseEventHandler(morseButton_PulseEnd);

            morseLetter = string.Empty;
            message = string.Empty;

            //InputPort button = new InterruptPort(Pins.GPIO_PIN_D8, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeLevelHigh);
            //OutputPort led = new OutputPort(Pins.GPIO_PIN_D9, false);

            //long previousButtonStateTicks = DateTime.Now.Ticks;
            //bool previousButtonState = button.Read();

            while (!stopButton.Pushed)
            {
            }



            //lcd.Clear();
            //lcd.Write("end");

        }

        static void morseButton_PulseEnd(object sender, PulseEventArgs e)
        {
            double deltaMs = e.Delta / TimeSpan.TicksPerMillisecond;
            Debug.Print("PulseEnd : " + deltaMs);

            if (deltaMs >= ExtraLongPulse)
            {
                if (message.Length > 0)
                    message = message.Substring(0, message.Length - 1);
                morseLetter = string.Empty;
                lcd.Clear();
                lcd.Write(message);
                return;
            }
            
            if (deltaMs >= LongPulse)
                morseLetter += "-";
            else
                morseLetter += ".";

            string letter = MorseEncoder.Decode(morseLetter);
            if (letter != null)
            {
                lcd.Clear();
                lcd.Write(message);
                lcd.Write(letter);
                lcd.MoveCursorLeft();
            }

            Debug.Print("morse : " + morseLetter);
        }

        static void morseButton_PulseStart(object sender, PulseEventArgs e)
        {
            double deltaMs = e.Delta / TimeSpan.TicksPerMillisecond;
            Debug.Print("PulseStart : " + deltaMs);

            if (deltaMs < ExtraLongPulse)
                return;

            string letter = MorseEncoder.Decode(morseLetter);
            Debug.Print(morseLetter + " => " + letter);

            if (letter == null)
                return;

            lcd.Write(letter);
            message += letter ;
            morseLetter = string.Empty;

        }

    }
}
